<?php
class Model_Procedure extends Model_Template
{
	private $className2;
	private $className3;
	
	public function __construct()
	{
		parent::__construct();
		$this->className = 'Model_Table_Procedure';
		$this->className2 = 'Model_Table_ProcedureHasAppointment';
		$this->className3 = 'Model_Table_ProcedureHasInventory';
	}
	
	///////////////////////////////////////////////////////////////////returns a list of all procedures
	public function getAllProcedures()
	{
		//Select All procedures query
		$sqlSelectAllProcedures = 'SELECT * FROM `procedure`;';
		
		//Prepare statement
		$selectProcedures = Controller_Template::$db->prepareEKADBPDO($sqlSelectAllProcedures, $this->className, array('procedure_id','name','duration'));
		
		//Execute statement
		$selectProcedures->execute();
		
		return $selectProcedures->fetchAll();
	}
	
	
	///////////////////////////////////////////////////////////////////create a procedure with all items assigned to it
	public function createNewProcedure($procedure,$itemsList,$quantityList)
	{
		//Create a procedure query
		$sqlCreateProcedure = 'INSERT INTO `procedure` (name,duration) VALUES (?,?);';
		
		//Prepare statement
		$createProcedure = Controller_Template::$db->prepareEKADBPDO($sqlCreateProcedure, $this->className, array('procedure_id','appointment_id'));
		
		//Execute statement
		$createProcedure->execute(array($procedure[1],$procedure[2]));
		
		$lastprocedureId=Controller_Template::$db->lastInsertId();
		
		$EmptyTestArray = array_filter($itemsList);
		
		if(!empty($EmptyTestArray))
		{
			for($i=0;$i<count($itemsList);$i++)
			{
				self::addItemToProcedure($lastprocedureId,$itemsList[$i],$quantityList[$i]);
			}
		}
		
		if($createProcedure->rowCount()==1)
		{
			return $lastprocedureId;
		}
	}
	
	///////////////////////////////////////////////////////////////////////////////deletes a procedure
	public function deleteProcedure($targetProcedure)
	{
		//Delete procedure sql
		$sqlDeleteProcedure='DELETE FROM `procedure` WHERE procedure_id=?';
		
		//Prepare statement
		$deleteProcedure = Controller_Template::$db->prepareEKADBPDO($sqlDeleteProcedure, $this->className, array('procedure_id','name','duration'));
		
		//Execute statement
		$deleteProcedure->execute(array($targetProcedure));
		
		return $deleteProcedure->rowCount();
	}
	
	
	///////////////////////////////////////////////////////////////////updates one procedure with all items assigned to it
	public function updateProcedure($procedure,$itemsList,$quantityList)
	{
		//Update a procedure query
		$sqlUpdateProcedure = 'UPDATE `procedure` SET name=?,duration=? WHERE procedure_id=?;';
		
		//Prepare statement
		$updateProcedure = Controller_Template::$db->prepareEKADBPDO($sqlUpdateProcedure, $this->className, array('procedure_id','name','duration'));
		
		//Execute statement
		$updateProcedure->execute(array($procedure[1],$procedure[2],$procedure[0]));
		
		self::removeAllItemsFromProcedure($procedure[0]);
		
		$EmptyTestArray = array_filter($itemsList);
		
		if(!empty($EmptyTestArray))
		{
			for($i=0;$i<count($itemsList);$i++)
			{
				self::addItemToProcedure($procedure[0],$itemsList[$i],$quantityList[$i]);
			}
		}
		
		return $updateProcedure->rowCount();
		
	}
	//////////////////////////////////////////////////////////////////returns all procedures that is assigned to one appointment
	public function getAllProceduresByAppointment($appointmentId)
	{
		//Select All procedures with the same appointment query
		$sqlSelectAllProcedures = 'SELECT * FROM Procedure_has_Appointment WHERE Appointment_appointment_id=?;';
	
		//Prepare statement
		$selectProcedures = Controller_Template::$db->prepareEKADBPDO($sqlSelectAllProcedures, $this->className2, array('procedure_id','appointment_id'));
	
		//Execute statement
		$selectProcedures->execute(array($appointmentId));
	
		return $selectProcedures->fetchAll();
	}
	
	//////////////////////////////////////////////////////////////////return the table that contains all item assigned to procedures
	public function getAllProcedureItems()
	{
		//Select All items with procedure id query
		$sqlSelectAllProcedureItems = 'SELECT * FROM Procedure_has_Inventory;';
	
		//Prepare statement
		$selectAllProcedureItems = Controller_Template::$db->prepareEKADBPDO($sqlSelectAllProcedureItems, $this->className3, array('procedure_id','inventory_id','quantity_needed'));
	
		//Execute statement
		$selectAllProcedureItems->execute();
	
		return $selectAllProcedureItems->fetchAll();
	}
	
	/////////////////////////////////////////////////////////////////////adds one item to a procedure
	public function addItemToProcedure($procedure,$item,$quantity)
	{
		//Add item to procedure query
		$sqlAddItemToProcedure = 'INSERT INTO Procedure_has_Inventory (Procedure_procedure_id,Inventory_inventory_id,quantity_needed) VALUES (?,?,?);';
		
		//Prepare statement
		$addItemToProcedure = Controller_Template::$db->prepareEKADBPDO($sqlAddItemToProcedure, $this->className3, array('procedure_id','inventory_id','quantity_needed'));
		
		//Execute statement
		$addItemToProcedure->execute(array($procedure,$item,$quantity));
		
		return $addItemToProcedure->rowCount();
	}
	
	
	//////////////////////////////////////////////////////////////////removes all items that are assigned to one procedure
	public function removeAllItemsFromProcedure($procedure)
	{
		
		//remove all items from procedure query
		$sqlRemoveAllItemsFromProcedure = 'DELETE FROM Procedure_has_Inventory WHERE Procedure_procedure_id=?;';
		
		//Prepare statement
		$removeAllItemsFromProcedure = Controller_Template::$db->prepareEKADBPDO($sqlRemoveAllItemsFromProcedure, $this->className3, array('procedure_id','inventory_id','quantity_needed'));
		
		//Execute statement
		$removeAllItemsFromProcedure->execute(array($procedure));
		
		return $removeAllItemsFromProcedure->rowCount();
	}

}